package com.yifang.yibingproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifang.yibingproject.dto.RoleDTO;
import com.yifang.yibingproject.dto.SysMenuTreeDTO;
import com.yifang.yibingproject.exception.ParamException;
import com.yifang.yibingproject.service.SysRoleService;
import com.yifang.yibingproject.utils.Log;
import com.yifang.yibingproject.utils.Result;
import com.yifang.yibingproject.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @Author: Lew
 * @DateTime: 2020/10/17 9:41 下午
 * @Description:
 */
@Api(tags = "系统角色")
@RestController
@RequestMapping("sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Log(value = "新增角色", resp = true)
    @ApiOperation("新增角色")
    @PostMapping("add")
    public Result<String> add(@ApiParam("名称") @RequestParam String name,
                              @ApiParam("描述") @RequestParam(required = false) String description) {
        name = name.trim();
        if (name.length() < 2) {
            throw new ParamException("名称少于2个字符");
        }
        return ResultUtils.data(sysRoleService.add(name, description));
    }

    @Log(value = "修改角色", resp = true)
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result<String> update(@ApiParam("商品分类编辑对象DTO") @RequestBody @Valid RoleDTO dto) {
        String name = dto.getName().trim();
        if (name.length() < 2) {
            throw new ParamException("名称少于2个字符");
        }
        return ResultUtils.data(sysRoleService.update(dto));
    }

    @ApiOperation("获取角色")
    @GetMapping("get/{id}")
    public Result<RoleDTO> get(@ApiParam("角色id") @PathVariable String id) {
        return ResultUtils.data(sysRoleService.get(id));
    }

    @Log(value = "删除角色", resp = true)
    @ApiOperation("删除角色")
    @DeleteMapping("del/{id}")
    public Result<String> del(@ApiParam("角色id") @PathVariable Long id) {
        return ResultUtils.data(sysRoleService.del(id));
    }

    @Log(value = "启用角色", resp = true)
    @ApiOperation("启用角色")
    @PostMapping("enable/{id}")
    public Result<String> enable(@ApiParam("角色id") @PathVariable String id) {
        return ResultUtils.data(sysRoleService.enable(id));
    }

    @Log(value = "禁用角色", resp = true)
    @ApiOperation("禁用角色")
    @PostMapping("disable/{id}")
    public Result<String> disable(@ApiParam("角色id") @PathVariable String id) {
        return ResultUtils.data(sysRoleService.disable(id));
    }

    @Log(value = "角色绑定权限", resp = true)
    @ApiOperation("绑定权限")
    @PostMapping("bindAuth")
    public Result<String> bindAuth(@ApiParam("角色id") @RequestParam Long roleId,
                                   @ApiParam("权限ID集合") @RequestParam Set<Long> authIds) {
        return ResultUtils.data(sysRoleService.bindAuth(roleId, authIds));
    }

    @ApiOperation("角色列表")
    @GetMapping("list")
    public Result<Page<RoleDTO>> list(@ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                      @ApiParam("数量") @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return ResultUtils.page(sysRoleService.list(pageNo, pageSize));
    }

    @ApiOperation("角色所有列表")
    @GetMapping("listAll")
    public Result<List<RoleDTO>> listAll() {
        return ResultUtils.data(sysRoleService.listAll());
    }

    @Log(value = "角色绑定菜单", resp = true)
    @ApiOperation("绑定菜单")
    @PostMapping("bindMenu")
    public Result<String> bindMenu(@ApiParam("角色id") @RequestParam Long roleId,
                                   @ApiParam("菜单ID集合") @RequestParam Set<Long> menuIds) {
        return ResultUtils.data(sysRoleService.bindMenu(roleId, menuIds));
    }

    @ApiOperation("角色菜单树形列表")
    @GetMapping("menuTree/{id}")
    public Result<List<SysMenuTreeDTO>> menuTreeByUserId(@ApiParam("角色id") @PathVariable Long id) {
        return ResultUtils.data(sysRoleService.menuTree(id));
    }

}
